﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Zgq.CommonTools
{
    public static class TypeHelper
    {

        public static int ToInt(this object obj)
        {
            if (obj != null && obj != DBNull.Value)
            {
                if (string.IsNullOrEmpty(obj.ToString()))
                {
                    return 0;
                }
                else
                {
                    return int.Parse(obj.ToString());
                }
            }
            return 0;
        }
        public static long ToLong(this object obj)
        {
            if (null != obj && obj != DBNull.Value)
            {
                if (string.IsNullOrEmpty(obj.ToString()))
                {
                    return 0;
                }
                return long.Parse(obj.ToString().Trim());
            }
            return 0;
        }


        public static double Todouble(this object obj) 
        {
            if (obj != null && obj != DBNull.Value)
            {
                if (string.IsNullOrEmpty(obj.ToString()))
                {
                    return 0.0;
                }
                else
                {
                    return double.Parse(obj.ToString());
                }
            }
            return  0.0;
        }


        public static decimal ToDecimal(this object obj)
        {
            if (obj != null && obj != DBNull.Value)
            {
                if (string.IsNullOrEmpty(obj.ToString()))
                {
                    return 0;
                }
                else
                {
                    return decimal.Parse(obj.ToString());
                }
            }
            return decimal.Zero;
        }

        public static string ToString(this object obj)
        {
            if (obj != null && obj != DBNull.Value)
            {
                return obj.ToString();
            }
            return string.Empty;
        }

        public static string ToStringExt(this object obj)
        {
            if (obj != null && obj != DBNull.Value)
            {
                return obj.ToString();
            }
            return string.Empty;
        }
        /// <summary>
        /// 把object转换成一个可为空的时间类型,如果Null则返回为空
        /// </summary>
        /// <param name="obj">Oject对像</param>
        /// <returns>返一个DateTime类型</returns>
        public static Nullable<DateTime> ToDataTimeNull(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return default(DateTime?);
            }
            return ToDataTime(obj);
        }
        public static DateTime ToDataTime(this object obj)
        {
            if (obj != null && obj != DBNull.Value)
            {
                if (string.IsNullOrEmpty(obj.ToString()))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return DateTime.Parse(obj.ToString());
                }
            }
            else
            { return DateTime.MinValue; }

        }

        public static bool ToBool(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return false;
            }
            string str = obj.ToString();

            string strUpper = str.ToUpper();
            if (strUpper == "Y")
            {
                return true;
            }
            if (strUpper == "N")
            {
                return false;
            }
            if (str.Trim() == "0")
            {
                return false;
            }
            if (str.Trim() == "1")
            {
                return true;
            }
            return bool.Parse(str);
        }

        public static SqlParameter[] ToArrayByC5(this List<SqlParameter> sqlParameterList)
        {
            foreach (var item in sqlParameterList)
            {
                if (item.Value == null)
                    item.Value = System.DBNull.Value;
            }
            return sqlParameterList.ToArray();
        }
    }
}
